package org.web.ssm.controller.wechatController;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.web.ssm.dto.ResultModel;
import org.web.ssm.model.ProductMapView;
import org.web.ssm.model.User;
import org.web.ssm.service.wechatService.IProductMapService;

import java.text.ParseException;

/**
 * Created by LUOLIANG on 2016/6/22.
 */
@RestController
@RequestMapping("/weProduct")
@SessionAttributes("currUser")
public class ProductMapController {
    @Autowired
    private IProductMapService productMapService;

    @RequestMapping(value = "/Statistics", method = RequestMethod.GET)
    public String Statistics(@ModelAttribute("currUser") User user) throws ParseException {
        return JSON.toJSONString(new ResultModel<ProductMapView>(true, productMapService.getStatistics(user.getUserid())));
    }

    @RequestMapping(value = "/payProduct", method = RequestMethod.GET)
    public String payProduct(@ModelAttribute("currUser") User user, @RequestParam("productid") String productid, @RequestParam("money") String money)
            throws ParseException {
        System.out.println(user + "/n" + productid + money);
        try {
            productMapService.insertOrUpdateProductMapping(user, productid, money);
            return JSON.toJSONString(new ResultModel<ProductMapView>("购买成功", true));
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(new ResultModel<ProductMapView>("购买失败", false));
        }
    }
}
